<?php

namespace app\admin\controller;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Db;
use think\Session;

class Appuser extends Base
{

    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = array();
            $map['user_status'] = array('<>',3);
            if(isset($department)&&$department != ""){
                $map['user_depart_id'] = $department;
            }
            if(isset($userFrom)&&$userFrom != ""){
                $map['user_where'] = $userFrom;
            }
            if(isset($key)&&$key!="")
            {
                $map['user_username|user_phone|user_zhiwei'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('u_user u')
                ->join('department d','u.user_depart_id = d.department_id','left')
                ->where($map)
                ->count();//计算总页面
            $lists = Db::name('u_user u')
                ->join('department d','u.user_depart_id = d.department_id','left')
                ->where($map)
                ->field('department_id,department_name,user_id,user_username,user_phone,user_zhiwei,user_role,user_where,user_status')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $data = Db::name('department')->select();
        return $this->fetch("appuser/index",['data'=>$data]);
    }


    /**
     * [userAdd 添加用户]
     * @return [type] [description]
     * @author
     */
    public function memberAdd()
    {
        if(request()->isPost()){
            extract(input());
            $param = [
                'user_username'=>$username,
                'user_image'=>$thumb,
                'user_phone'=>$phone,
                'user_gender'=>$sex,
                'user_birthday'=>$birthday,
                'user_depart_id'=>$department,
                'user_zhiwei'=>$zhiwei,
                'user_zhize'=>$zhize,
                'user_time'=>date("Y-m-d H:i:s"),
                'user_password'=>md5($password),
                'user_role'=>$role,
                'user_where'=>2
            ];
            if($role == 2){
                $param['warehouse_id'] = $house;
            }
            $res = Db::name('u_user')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $data = Db::name('department')->select();
        $house = Db::name('warehouse')->field('house_id,warehouse_name')->select();
        return $this->fetch('appuser/useradd',['data'=>$data,'house'=>$house]);
    }


    /**
     * [userEdit 编辑用户]
     * @return [type] [description]
     * @author
     */
    public function memberEdit()
    {
        extract(input());
        if(request()->isPost ()){
            $param = [
                'user_username'=>$username,
                'user_image'=>$thumb,
                'user_phone'=>$phone,
                'user_gender'=>$sex,
                'user_birthday'=>$birthday,
                'user_depart_id'=>$department,
                'user_zhiwei'=>$zhiwei,
                'user_zhize'=>$zhize,
                'warehouse_id' => $warehouse,
                'user_update_time'=>date('Y-m-d H:i:s')
            ];
            if($password != ''){
                $param['user_password'] = $password;
            }
            $res = Db::name('u_user')->where('user_id',$user_id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $info = Db::name('u_user u')
            ->join('department d','u.user_depart_id = d.department_id','left')
            ->where(['user_id'=>$id])
            ->field(['user_id','user_username',"if(user_image!='',concat('".http_type ()."',user_image),user_image)"=>'image','user_image','user_phone','user_gender','user_birthday','user_depart_id','user_zhiwei','user_zhize','user_password','department_id', 'warehouse_id'])
            ->find();
        $data = Db::name('department')->select();
        $warehouse = Db::name('warehouse')->select();
        return $this->fetch('appuser/useredit',['info'=>$info,'data'=>$data, 'warehouse'=>$warehouse]);
    }


    /**
     * [UserDel 删除用户]
     * @return [type] [description]
     * @author
     */
    public function memberDel()
    {
        $id = input('id');
        $res = Db::name('u_user')->where(['user_id'=>$id])->setField ('user_status',3);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /**
     * [user_state 用户状态]
     * @return [type] [description]
     * @author
     */
    public function memberState()
    {
        extract(input());
        if($num == 2){
            $msg = '禁用';
        }else{
            $msg = '启用';
        }
        $res = Db::name('u_user')
            ->where(['user_id'=>$id])
            ->setField ('user_status',$num);
        if($res){
//            return ['code' => 200,  'msg' => '已'.$msg];
        }else{
            return ['code' => 100, 'msg' => $msg.'失败'];
        }
    }


    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function excelMember(){
        extract(input());
        $map = [];
        $map['user_status'] = ['<>',3];
        if($ids =="" && $key == "" && $role == "" && $userFrom == "" && $department ==""){
            $data = Db::name('u_user u')
                ->join('department d','u.user_depart_id = d.department_id','left')
                ->join('warehouse w','u.warehouse_id = w.house_id','left')
                ->field(['user_username','user_phone',"if(user_gender!=1,'女','男')"=>'user_gender','user_birthday','department_name','user_zhiwei','user_zhize','user_time',"if(user_role!=1,'库管员','入库员')"=>'user_role','warehouse_name'])
                ->where($map)
                ->select();
        }
        if($ids != ""){
            $ids = trim($ids,',');
            $ids = explode(',',$ids);
            $data = Db::name('u_user u')
                ->join('department d','u.user_depart_id = d.department_id','left')
                ->join('warehouse w','u.warehouse_id = w.house_id','left')
                ->field(['user_username','user_phone',"if(user_gender!=1,'女','男')"=>'user_gender','user_birthday','department_name','user_zhiwei','user_zhize','user_time',"if(user_role!=1,'库管员','入库员')"=>'user_role','warehouse_name'])
                ->where($map)
                ->where('user_id','in',$ids)
                ->select();
        }else{
            if($role != ""){
                $map['user_role'] = $role;
            }
            if($key!="")
            {
                $map['user_username|user_phone|user_zhiwei'] = ['like',"%" . $key . "%"];
            }
            if($userFrom != ""){
                $map['user_where'] = $userFrom;
            }
            if($department != ""){
                $map['user_depart_id'] = $department;
            }
            $data = Db::name('u_user u')
                ->join('department d','u.user_depart_id = d.department_id','left')
                ->join('warehouse w','u.warehouse_id = w.house_id','left')
                ->field(['user_username','user_phone',"if(user_gender!=1,'女','男')"=>'user_gender','user_birthday','department_name','user_zhiwei','user_zhize','user_time',"if(user_role!=1,'库管员','入库员')"=>'user_role','warehouse_name'])
                ->where($map)
                ->select();
        }
        $cellname = [
            ['user_username','用户名',15,'LEFT'],
            ['user_phone','手机号',15,'LEFT'],
            ['user_gender','性别',15,'LEFT'],
            ['user_birthday','出生日期',15,'LEFT'],
            ['department_name','部门名称',15,'LEFT'],
            ['user_zhiwei','职位',15,'LEFT'],
            ['user_zhize','职责',15,'LEFT'],
            ['user_time','注册时间',15,'LEFT'],
            ['user_role','角色',15,'LEFT'],
            ['warehouse_name','仓库',15,'LEFT']
        ];
        $res = exportExcel('用户信息','admin',$cellname,$data);
        return json($res);
    }

}